import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import '../models/user.dart';
import '../services/auth_service.dart';
import '../services/web_auth_service.dart';

/// 个人信息页面状态管理
class ProfileProvider extends ChangeNotifier {
  // 根据平台选择合适的认证服务
  late final dynamic _authService;

  ProfileProvider() {
    if (kIsWeb) {
      _authService = WebAuthService();
    } else {
      _authService = AuthService();
    }
  }

  // 状态变量
  bool _isLoading = false;
  bool _isEditing = false;
  String? _errorMessage;
  User? _user;

  // Getters
  bool get isLoading => _isLoading;
  bool get isEditing => _isEditing;
  String? get errorMessage => _errorMessage;
  User? get user => _user;

  // 设置用户信息
  void setUser(User? user) {
    _user = user;
    notifyListeners();
  }

  // 切换编辑模式
  void toggleEditing() {
    _isEditing = !_isEditing;
    _errorMessage = null;
    notifyListeners();
  }

  // 清除错误
  void clearError() {
    _errorMessage = null;
    notifyListeners();
  }

  // 更新个人信息
  Future<bool> updateProfile({
    required String name,
    required String email,
    String? bio,
    String? phone,
    String? location,
  }) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      // 调用真实的API更新用户信息
      final response = await _authService.updateProfile(
        name: name,
        bio: bio,
        phone: phone,
        location: location,
      );

      if (response != null) {
        // 更新本地用户信息
        _user = response;
        _isEditing = false;

        // 🔥 关键：同步更新AuthProvider中的用户数据
        await _syncUserDataWithAuthProvider(response);

        return true;
      } else {
        _errorMessage = '更新失败：服务器响应为空';
        return false;
      }
    } catch (e) {
      _errorMessage = '更新失败: $e';
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 同步用户数据到AuthProvider
  Future<void> _syncUserDataWithAuthProvider(User updatedUser) async {
    try {
      // 这里需要通过全局状态或事件通知AuthProvider更新
      // 我们将在SimpleProfilePage中处理这个同步
    } catch (e) {
      print('同步用户数据失败: $e');
    }
  }

  // 修改密码
  Future<bool> changePassword({
    required String currentPassword,
    required String newPassword,
  }) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      // 模拟API调用
      await Future.delayed(const Duration(seconds: 1));

      // 这里应该调用实际的密码修改API
      return true;
    } catch (e) {
      _errorMessage = '密码修改失败: $e';
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 上传头像
  Future<bool> uploadAvatar(String imagePath) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      // 模拟API调用
      await Future.delayed(const Duration(seconds: 1));

      // 这里应该调用实际的头像上传API
      return true;
    } catch (e) {
      _errorMessage = '头像上传失败: $e';
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 退出登录
  Future<void> logout() async {
    try {
      await _authService.logout();
      _user = null;
      notifyListeners();
    } catch (e) {
      _errorMessage = '退出登录失败: $e';
      notifyListeners();
    }
  }
}
